#include "object_pool.hpp"
#include <cstring>
#include <thread>

using namespace objpool;

class Frame;
using ObjectPoolPtr = std::shared_ptr<ObjectPool<Frame>>;
class Frame : std::enable_shared_from_this<Frame>
{
public:
    explicit Frame(size_t maxSz)
    {
        data = std::unique_ptr<char[]>(new char[maxSz]);
        maxSize = maxSz;
    }
    ~Frame()
    {
        std::cout << "~Frame" << std::endl;
    }
    void Reset()
    {
        bzero(data.get(), maxSize);
    }
    void Fill(char* buf, size_t len)
    {
        memcpy(data.get(), buf, len);       // TODO: use security function
    }
private:
    size_t maxSize;
    size_t used;
    std::unique_ptr<char[]> data;
};


size_t initSz = 10;
size_t maxSz = 20;
size_t frameSz = 64;
ObjectPool<Frame> pool(
    initSz,
    maxSz,
    true,
    []()->Frame* {return new Frame(frameSz); }
);

void test()
{
    int cnt = 20;
    while(cnt--)
    {
        auto obj = pool.BorrowOne();
        auto obj1 = pool.BorrowOne();
        auto lst = pool.BorrowMore(101);
        auto lst2 = pool.BorrowMore(11);
        lst.clear();
    }
}

int main()
{
    std::thread t1(test), t2(test);
    t1.join();
    t2.join();
    pool.UseInfo();
    pool.Finish();
    std::cout << "pause" << std::endl;
    return 0;
}

